package com.cnpc.rightslogan.controller;

import com.alibaba.fastjson.JSON;
import com.cnpc.rightslogan.entity.User;
import com.cnpc.rightslogan.service.IUserService;
import com.cnpc.rightslogan.utils.common.BaseController;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by Administrator on 2017/9/13.
 */
@RestController
@RequestMapping("userInfo")
@Api("UserInfoController相关的api")
@Slf4j
public class UserInfoController extends BaseController {

    @Autowired
    private IUserService userInfoServiceImpl;

    @ApiOperation(value = "获取所有数据，分页获取", notes = "获取所有数据")
    @ResponseBody
    @PostMapping("/user/list")
    public PageInfo<User> getUserList(@RequestBody @ApiParam(name = "PageInfo", value = "分页对象") PageInfo pageInfo,
                                      @Validated @RequestBody @ApiParam(name = "User", value = "User对象") User user) {
        log.info("获取所有数据");
        return userInfoServiceImpl.getUserList(pageInfo.getPageNum(), pageInfo.getPageSize(), user);
    }

    @ApiOperation(value = "获取所有数据，分页获取", notes = "获取所有数据")
    @ResponseBody
    @PostMapping("/user/list1")
    public PageInfo<User> getUserList1(@RequestBody @ApiParam(name = "map", value = "分页对象及User对象") Map map) {
        String json = JSON.toJSONString(map);
        PageInfo pageInfo = JSON.parseObject(json, PageInfo.class);
        User user = JSON.parseObject(json, User.class);
        log.info("获取所有数据");
        return userInfoServiceImpl.getUserList(pageInfo.getPageNum(), pageInfo.getPageSize(), user);
    }

    @ApiOperation(value = "获取所有数据，分页获取", notes = "获取所有数据")
    @ResponseBody
    @PostMapping("/user/list2")
    public PageInfo<User> getUserList2(@RequestBody @ApiParam(name = "json", value = "分页对象及User对象json串") String json) {
        PageInfo pageInfo = JSON.parseObject(json, PageInfo.class);
        User user = JSON.parseObject(json, User.class);
        log.info("获取所有数据");
        return userInfoServiceImpl.getUserList(pageInfo.getPageNum(), pageInfo.getPageSize(), user);
    }

    @ResponseBody
    @ApiOperation(httpMethod = "POST", value = "精确查询", notes = "精确查询")
    @PostMapping("/user/condition")
    public User getUser(@RequestBody @ApiParam(name = "User", value = "User对象") User user) {
        log.info("获取所有数据");
        return userInfoServiceImpl.getUser(user);
    }

}